<template>
  <div>
    <div class="hello">
      test
    </div>
  </div>
</template>

<script type="text/javascript">
import Stomp from "stompjs";
// 定义常量
export const IP = "10.243.203.100" // 自己的mq服务ip
export const PORT = "15674"
export const MQTT_SERVICE = 'ws://' + IP + ':' + PORT + '/ws' // mqtt服务地址
export const MQTT_USERNAME = 'admin' // 连接用户名, todo: read from database
export const MQTT_PASSWORD = 'admin' //  连接密码, todo: read from database
export const VIRTUAL_HOST = '/' //  侦听器端口
export default {
  data () {
    return {
      client: ''
    };
  },
  created () {
    this.connect();
  },
  methods: {
    // 消息队列获取
    connect () {
      let ws = new WebSocket(MQTT_SERVICE); // new SockJS("http://xx.108.xx.33:15674/stomp")
      this.client = Stomp.over(ws);
      //初始化连接
      const headers = {
        login: MQTT_USERNAME,
        passcode: MQTT_PASSWORD
      };
      //进行连接
      this.client.connect(headers.login, headers.passcode, this.onConnected, this.onFailed, VIRTUAL_HOST);
    },
    onConnected: function () {
      //订阅频道
      const topic = '/queue/delayQueue';
      this.client.subscribe(topic, this.responseCallback, this.onFailed);
    },
    // 回传消息

    responseCallback: function (frame) {
      console.log("MQ msg=>" + frame.body);
      //接收消息处理
    },
    onFailed: function (frame) {
      console.log("MQ Failed: " + frame);
    },
    // 断开相应的连接
    close: function () {
      this.client.disconnect(function () {
        console.log("已退出账号");
      })
    }
  }
}
</script>

<style scoped>
</style>

